package com.tjbank.cssys.web.controller;


import com.tjbank.cssys.domain.CkLevel;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CkSmsDescribe;
import com.tjbank.cssys.web.service.CkLevelService;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CkSmsDescribeService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping
@Api(tags = "CkEnumController", description = "枚举管理")
public class CkEnumController {
    @Autowired
    private CkLevelService ckLevelService;
    @Autowired
    private CkSmsDescribeService ckSmsDescribeService;

    @Autowired
    private CkLogService ckLogService;

     private static  final Logger logger =LoggerFactory.getLogger(CkEnumController.class);

    /**
     * 获取是否逾期
     * @return
     */
    @ApiOperation("获取是否逾期")
    @GetMapping("/U_GetIsOverdue")
    public CommonResult getIsOverdue(HttpServletRequest request) {

        List<CkLevel> pnames =ckLevelService.findByCategory((byte)4);
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取是否逾期异常",e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取是否逾期");

        if(CollectionUtils.isNotEmpty(pnames)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pnames);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }

    }

    /**
     * 获取下发方向
     * @return
     */
    @ApiOperation("获取下发方向")
    @GetMapping("/U_GetDistributionDirection")
    public CommonResult getDistributionDirection(HttpServletRequest request) {
        List<CkLevel> pnames =ckLevelService.findByCategory((byte)5);
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取下发方向异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取下发方向");

        if(CollectionUtils.isNotEmpty(pnames)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pnames);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }
    }
    /**
     * 获取风险等级
     * @return
     */
    @ApiOperation("获取风险等级")
    @GetMapping("/U_GetRiskLevel")
    public CommonResult getRiskLevel(HttpServletRequest request) {
        List<CkLevel> pnames =ckLevelService.findByCategory((byte)3);
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取风险等级");
        if(CollectionUtils.isNotEmpty(pnames)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pnames);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }
    }
    /**
     * 获取金额
     * @return
     */
    @ApiOperation("获取金额")
    @GetMapping("/U_GetAmountMoney")
    public CommonResult getAmountMoney(HttpServletRequest request) {
        List<CkLevel> pnames =ckLevelService.findByCategory((byte)2);
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取金额");
        if(CollectionUtils.isNotEmpty(pnames)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pnames);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }
    }
    /**
     * 获取逾期等级
     * @return
     */
    @ApiOperation("获取逾期等级")
    @GetMapping("/U_GetOverdueGrade")
    public CommonResult getOverdueGrade( HttpServletRequest request) {
        List<CkLevel> pnames =ckLevelService.findByCategory((byte)1);
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        ckLog.setTimes(new Date());
        ckLog.setOperationType("获取逾期等级");
        if(CollectionUtils.isNotEmpty(pnames)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pnames);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }
    }
    /**
     * 获取逾期等级
     * @return
     */
    @ApiOperation("获取短信模板")
    @GetMapping("/U_GetSmsContext")
    public CommonResult getSmsContext(HttpServletRequest request) {
        List<CkSmsDescribe> ckSmsDescribes = ckSmsDescribeService.selectList();
        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        ckLog.setTimes(new Date());
        ckLog.setOperationType("获取短信模板");
        if(CollectionUtils.isNotEmpty(ckSmsDescribes)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(ckSmsDescribes);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("错误");
        }
    }
}
